/*
 * @Author: 41
 * @Date: 2022-02-17 18:34:12
 * @LastEditors: spicylemon
 * @LastEditTime: 2022-03-06 16:15:40
 * @Description: 
 */
import { createRouter, createWebHistory } from 'vue-router'
const routerHistory = createWebHistory()

const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/',
      component: () => import('@/pages/Home.vue')
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/pages/Login.vue')
    },
    {
      name: 'home',
      path: '/home',
      component: () => import('@/pages/Home.vue')
    },
    {
      name: 'section',
      path: '/section/:id/:title',
      component: () => import('@/pages/Section.vue'),
      props: true
    },
    {
      name: 'search',
      path: '/search',
      component: () => import('@/pages/Search.vue')
    },
    {

      path: '/article/:id',
      component: () => import('@/pages/Article.vue'),
    },

    {
      name: 'checktoken',
      path: '/checktoken',
      component: () => import('@/pages/checktoken.vue')
    },
    {
      name: 'userHome',
      path: '/userhome',
      component: () => import('@/pages/UserHome.vue')
    },
    {
      name: 'searchPage',
      path: '/searchpage/:searchValue',
      component: () => import('@/pages/Search.vue'),
      props: true
    },
    {
      name: 'otherUserPage',
      path: '/others/:id',
      component: () => import('@/pages/otherUserPage.vue'),
      props: true
    },
    {
      path: '/publish',
      component: () => import('@/pages/Publish.vue'),
    }
  ]
})
//创建路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to)
  let token = null;
  if (to.path === '/login') {
    next();
    return;
  } else {
    token = localStorage.getItem('Authorization');
    console.log(token);
  }
  if (!token || token === '') {
    console.log('进入路由守卫' + 'token为：' + token)
    next('/login')
    // next()
  } else {
    next();
  }
  // if (to.path !== '/login') {
  //   try {
  //     // 这里引入有点小bug先写个思路
  //     // 得到新token说明token有效 无效直接进入登录页面了
  //     // 请求/users/updatatoken  res.result.newtoken为新token
  //     // 如果获得新token，local存储新token
  //     // 这样每次访问初始地址的时候都会读取一次新token
  //     next('/home')
  //   } catch {
  //     next()
  //   }
  // }
  // 如果是登录页,直接放行
  // if (to.path === '/login') {

  //   next();
  //   return;
  // }
  // // 登录页之外,我们更新一次token,用sessionstorage控制每次登录只更新一次
  // else {
  //   next();
  // }
  // if (token === null || token === '') {
  //   console.log('进入路由守卫' + 'token为：' + token)
  //   next('/login')
  // } else {
  //   next();
  // }
});
export default router
